package 我的Java学习_算法基础.day_05;

import java.util.Arrays;

public class _071_变形词问题 {
    public static void main(String[] args) {
        String a = "aac",b="aca";
        System.out.println(checkSame(a,b));
    }
    //字符串转字符数组，排序比较 O(NlgN)
    static boolean checkSame(String a, String b) {
        int n1 = a.length();
        int n2 = b.length();
        if (n1 != n2) {
            return false;
        }
        char[] arr1 = a.toCharArray();
        char[] arr2 = b.toCharArray();
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }
    //若字符都是由ASCll码,创建辅助数组   O(N)
    static boolean checkSame2(String a,String b){
        int n1 = a.length();
        int n2 = b.length();
        if (n1 != n2) {
            return false;
        }
        int[] helper = new int[256];
        for(int i = 0;i<n1;i++){
            int t = (int)(a.charAt(i));
            helper[t]++;
        }
        for(int j =0;j<n1;j++){
            int f = (int)(b.charAt(j));
            if(helper[f]<=0){
                return false;
            }else
                helper[f]--;
        }
        return true;

    }
}
